import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../MyButton.dart';

class Storage {
  static Future<void> setString(k,v) async{
     SharedPreferences sp = await SharedPreferences.getInstance();
     sp.setString(k,v);
  } 
  static Future<String> getString(k) async{
     SharedPreferences sp = await SharedPreferences.getInstance();
     return sp.getString(k);
  } 
  static Future<void> remove(k) async{
     SharedPreferences sp = await SharedPreferences.getInstance();
     return sp.remove(k);
  } 
}

class StoragePage extends StatefulWidget {
  StoragePage({Key key}) : super(key: key);

  _StoragePageState createState() => _StoragePageState();
}

class _StoragePageState extends State<StoragePage> {
  String _name;
  _seveData() {
    Storage.setString("username", '张三');
  }
  _getData() async{
    var name = await Storage.getString("username");
    print(name);
    setState(() {
      _name = name;
    });
  }
  _removeData() async{
    Storage.remove('username');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("StoragePage"),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            MyButton(text: '保存数据',pressed: _seveData,),
            MyButton(text: '获取数据',pressed: _getData,),
            MyButton(text: '删除数据',pressed: _removeData,),
            Center(
              child: Text('${_name}'),
            )
          ],
        ),
      ),
      
    );
  }
}

